package com.xjrsoft.module.customerTwo.AppManage.service_contract.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.page.ConventPage;
import java.util.List;
import com.xjrsoft.common.utils.DataTransUtil;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.module.customerTwo.AppManage.service_contract.dto.ServiceContractDto;
import com.xjrsoft.module.customerTwo.AppManage.service_contract.dto.SaveServiceContractFormDataDto;
import com.xjrsoft.module.customerTwo.AppManage.service_contract.vo.ServiceContractVo;
import com.xjrsoft.module.customerTwo.AppManage.service_contract.vo.ServiceContractListVo;
import com.xjrsoft.module.customerTwo.AppManage.service_contract.dto.ServiceContractListDto;
import com.xjrsoft.core.secure.utils.SecureUtil;
import com.xjrsoft.core.tool.utils.FlowableUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;

import org.springframework.web.bind.annotation.*;
import com.xjrsoft.module.customerTwo.AppManage.service_contract.entity.ServiceContract;

import com.xjrsoft.module.customerTwo.AppManage.service_contract.service.IServiceContractService;

/**
 *  控制器
 *
 * @author lisen
 * @since 2024-04-25
 */
@RestController
@AllArgsConstructor
@RequestMapping("/service_contract")
@Api(value = "服务合同", tags = "服务合同接口")
public class ServiceContractController {


	private final IServiceContractService serviceContractService;


	/**
	 * 详情 [{fieldName=sales_department_id, lazyType=department}]
	 */
	@GetMapping("/{id}")
	@ApiOperation(value = "详情", notes = "传入serviceContract")
	//@SaCheckPermission(value = {"ServiceContract:view", "ServiceContract:modify"}, mode = SaMode.OR)
	@SaCheckLogin
	public Response<ServiceContractVo> getFormData(@PathVariable String id) {
		// 主表数据
		ServiceContract serviceContract = serviceContractService.getById(id);
		ServiceContractVo serviceContractVo = BeanUtil.copy(serviceContract, ServiceContractVo.class);
		serviceContractVo.setSalesDepartmentId_Name(DataTransUtil.getDepartmentNameById(serviceContractVo.getSalesDepartmentId()));
		return Response.ok(serviceContractVo);
	}

	/**
	 * 自定义分页 
	 */
	@GetMapping
	@ApiOperation(value = "分页", notes = "传入serviceContract")
		public Response<PageOutput<ServiceContractListVo>> getPageList(ServiceContractListDto listDto) {
		IPage<ServiceContract> page = serviceContractService.getPageList(listDto);
		List<ServiceContractListVo> records = BeanUtil.copyList(page.getRecords(), ServiceContractListVo.class);
			// 转换列表数据
		DataTransUtil.transListShowData(records);
			// 工作流数据处理
		FlowableUtil.systemFormList(records, SecureUtil.getModuleId());
		return Response.ok(ConventPage.getPageOutput(page.getTotal(), records));
	}

	/**
	 * 新增 
	 */
	@PostMapping
	@ApiOperation(value = "新增", notes = "传入serviceContract")
	//@SaCheckPermission("ServiceContract:add")
	@SaCheckLogin
	public Response save(@RequestBody SaveServiceContractFormDataDto formDto) {
		ServiceContractDto serviceContractDto = formDto.getServiceContractDto();
		ServiceContract serviceContract = BeanUtil.copy(serviceContractDto, ServiceContract.class);
		Integer dataId = serviceContractService.addServiceContract(serviceContract);
		return dataId == null ? Response.notOk() : Response.ok(dataId);
	}

	/**
	 * 修改 
	 */
	@PutMapping("/{id}")
	@ApiOperation(value = "修改", notes = "传入serviceContract")
	//@SaCheckPermission("ServiceContract:modify")
	@SaCheckLogin
	public Response update(@PathVariable String id, @RequestBody SaveServiceContractFormDataDto formDto) {
		ServiceContractDto serviceContractDto = formDto.getServiceContractDto();
		ServiceContract serviceContract = BeanUtil.copy(serviceContractDto, ServiceContract.class);
		return Response.status(serviceContractService.updateServiceContract(id, serviceContract));
	}



	/**
	 * 删除 
	 */
	@DeleteMapping("/{ids}")
	@ApiOperation(value = "删除", notes = "传入ids")
	//@SaCheckPermission(value = {"ServiceContract:batchDelete", "ServiceContract:delete"}, mode = SaMode.OR)
	@SaCheckLogin
	public Response remove(@ApiParam(value = "主键集合", required = true) @PathVariable String ids) {
		return Response.status(serviceContractService.delete(ids));
	}


}
